<?php

// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2030 http://www.jmzh1688.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: lxl
// +----------------------------------------------------------------------


namespace app\test22\controller;

use think\Controller;
use think\Db;

use app\test22\model\TestModel;


class Test extends Base
{


    public function index()
    {


        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $testmodel = new TestModel();


            $where = getWhereForm( $param);
            $where['isdelete']=0;

            $list = $testmodel->getByWhere($where, $offset, $limit);
            foreach($list as $key=>$vo){
                $list[$key]['operate'] = $this->getRowButton($vo['id']);
            }
            $result['total'] =$testmodel->getAllCount($where);  // 总数据
            $result['rows'] = $list;
            return json($result);
        }
        return $this->fetch();

    }

    public function del()
    {
        $testmodel = new TestModel();
        if(request()->isPost()){
            $id = input("post.id");
            $msg = $testmodel->delById($id);
            return json(msg($msg['code'], $msg['data'], $msg['msg']));
        }
        return $this->fetch();
    }

    public function edit()
    {
        $testmodel = new TestModel();

        if(request()->isPost()){
            $param = input("post.");

 
            foreach ($param as $key=>$value){
                if(is_array($value)&&count($value)>1)
                {
                    $param[$key]=implode(",",$param[$key]);
                }
            }
           

            $msg = $testmodel->edit($param);
            return json(msg($msg['code'], $msg['data'], $msg['msg']));
        }
      
        $id = input('param.id');
        $info=$testmodel->getById($id);
  
        $this->assign([
            'info' => $info
        ]);
        return $this->fetch();
    }

    public function add()
    {
    

        if(request()->isPost()){
            $testmodel = new TestModel();
            $param = input("post.");


            $msg = $testmodel->add($param);
            return json(msg($msg['code'], $msg['data'], $msg['msg']));
        }
    
        return $this->fetch();
    }



    
    /**
     * 操作按钮
     * @param $id
     */
    private function getRowButton($id)
    {
     
        $buttonlist= " <a href='edit?id=".$id."' class='btn btn-outline-primary btn-sm mod'>编辑</a> "
        ." <button class='btn btn-outline-primary btn-sm' onclick='del(this,".$id.");' >删除</button> ";
       return $buttonlist;
    }

    
}
